package com.fairchild.util;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class DesUtil {
	
	public static String getDecryptString(String str) {
		BASE64Decoder base64De = new BASE64Decoder();
		byte [] bt = null;
		String decrypt = null;
		try {
			bt = Hex.decodeHex(str.toCharArray());
			bt = base64De.decodeBuffer(new String(bt, "utf-8"));
			decrypt = new String(bt, "utf-8");
		} catch (DecoderException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return decrypt;
	}
	
	public static String getEncryptString(String str) {
		BASE64Encoder base64en = new BASE64Encoder();
		String encodeStr = "";
		try {
			encodeStr = base64en.encode(str.getBytes("utf-8"));
			encodeStr = Hex.encodeHexString(encodeStr.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encodeStr;
	}
	
	public static void main(String[] args) throws Exception {
		System.out.println(getEncryptString("root"));
		System.out.println(getEncryptString("123456"));
	}

}
